﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject;
using SmartProject.Entity;
using SmartProject.Data;
using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test01_Logger
    {
        private const string _className = "SmartProject.Testing.Test01_Logger";
        [Test]
        public void Test01_LogDebug()
        {
            // Delete all debugging messages first.
            using (var session = DataProvider.Instance.Session)
            {
                IList<Log> logs = Log.GetLogs(session, "Debug");
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < logs.Count; i++)
                    {
                        logs[i].Remove(session);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                Log.Debug(session, this, "AddLog", "TestDebug");
                logs = Log.GetLogs(session, "Debug");
                Assert.AreEqual(1, logs.Count);
                Log log1 = logs[0];
                Assert.AreEqual(log1.ClassName, _className);
                Assert.AreEqual(log1.MethodName, "AddLog");
                Assert.AreEqual(log1.Description, "TestDebug");
                Assert.AreEqual(log1.Active, true);
                Assert.AreEqual(log1.Severity.Description, "Debug");
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test02_LogError()
        {
            using (var session = DataProvider.Instance.Session)
            {
                // Delete all error messages first.
                IList<Log> logs = Log.GetLogs(session, "Error");
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < logs.Count; i++)
                    {
                        logs[i].Remove(session);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                Log.Error(session, this, "AddLog", "TestError");
                logs = Log.GetLogs(session, "Error");
                Assert.AreEqual(1, logs.Count);
                Log log1 = logs[0];

                Assert.AreEqual(log1.ClassName, _className);
                Assert.AreEqual(log1.MethodName, "AddLog");
                Assert.AreEqual(log1.Description, "TestError");
                Assert.AreEqual(log1.Active, true);
                Assert.AreEqual(log1.Severity.Description, "Error");
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test03_LogInfo()
        {
            using (var session = DataProvider.Instance.Session)
            {
                // Delete all info messages first.
                IList<Log> logs = Log.GetLogs(session, "Info");
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < logs.Count; i++)
                    {
                        logs[i].Remove(session);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                Log.Info(session, this, "AddLog", "TestInfo");
                logs = Log.GetLogs(session, "Info");
                Assert.AreEqual(1, logs.Count);
                Log log1 = logs[0];

                Assert.AreEqual(log1.ClassName, _className);
                Assert.AreEqual(log1.MethodName, "AddLog");
                Assert.AreEqual(log1.Description, "TestInfo");
                Assert.AreEqual(log1.Active, true);
                Assert.AreEqual(log1.Severity.Description, "Info");
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test04_LogWarn()
        {
            using (var session = DataProvider.Instance.Session)
            {
                // Delete all warning messages first.
                IList<Log> logs = Log.GetLogs(session, "Warn");
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < logs.Count; i++)
                    {
                        logs[i].Remove(session);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                Log.Warn(session, this, "AddLog", "TestWarn");
                logs = Log.GetLogs(session, "Warn");
                Assert.AreEqual(1, logs.Count);
                Log log1 = logs[0];

                Assert.AreEqual(log1.ClassName, _className);
                Assert.AreEqual(log1.MethodName, "AddLog");
                Assert.AreEqual(log1.Description, "TestWarn");
                Assert.AreEqual(log1.Active, true);
                Assert.AreEqual(log1.Severity.Description, "Warn");
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test05_LogException()
        {
            using (var session = DataProvider.Instance.Session)
            {
                // Delete all exception messages first.
                IList<Log> logs = Log.GetLogs(session, "Exception");
                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < logs.Count; i++)
                    {
                        logs[i].Remove(session);
                    }
                    transaction.Commit();
                    transaction.Dispose();
                }
                Exception someException = new Exception("SomeException");
                someException.Source = "Source";
                Log.Exception(session, this, someException);
                logs = Log.GetLogs(session, "Exception");
                Assert.AreEqual(1, logs.Count);
                Log log1 = logs[0];

                Assert.AreEqual(log1.ClassName, _className);
                Assert.AreEqual(log1.MethodName, "Source");
                Assert.AreEqual(log1.Description, "SomeException");
                Assert.AreEqual(log1.Active, true);
                Assert.AreEqual(log1.Severity.Description, "Exception");
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test06_DeleteInActive()
        {
            using (var session = DataProvider.Instance.Session)
            {
                Log log = new Log();
                using (var transaction = session.BeginTransaction())
                {
                    Log.Clean(session);
                    transaction.Commit();
                    transaction.Dispose();
                }

                IList<Log> logs = Log.GetLogs(session, "Exception");
                Assert.AreEqual(1, logs.Count);
                logs = Log.GetLogs(session, "Warn");
                Assert.AreEqual(1, logs.Count);
                logs = Log.GetLogs(session, "Info");
                Assert.AreEqual(1, logs.Count);
                logs = Log.GetLogs(session, "Error");
                Assert.AreEqual(1, logs.Count);
                logs = Log.GetLogs(session, "Debug");
                Assert.AreEqual(1, logs.Count);

                session.Close();
                session.Dispose();
            }
        }
    }
}
